import {normalize, schema} from 'normalizr'

const myData = {
  "success": true,
  "articles": [
    {
      "id": "001",
      "title": "test1",
      "author": {
        "id": '1',
        "name": 'Paul'
      },
      "content": "The day broke gray and dull",
      "time": "2018-11-29",
      "comments": [{
        "id": "100",
        "content": "this is content2",
        "commenter": {
          "id": '2',
          "name": 'Nicole',
        }
      },
        {
          "id": "101",
          "content": "this is content3",
          "commenter": {
            "id": '3',
            "name": 'Louu',
          }
        }
      ]
    },
    {
      "id": "002",
      "title": "test2",
      "author": {
        "id": '9',
        "name": 'Kobe'
      },
      "content": "She glanced mechanically at the house opposite, a stucco house with a portico, and went to the child’s bed.",
      "time": "2019-11-29",
      "comments": [{
        "id": "200",
        "content": "this is content2",
        "commenter": {
          "id": '2',
          "name": 'Nicole',
        }
      }]
    }
  ]
}

//预期的结果是
// {
//   "articles":[{}],
//   "comments":[{}],
//   "users":[{}],
// }

//第一步预期 先需求声明预期的格式 { articles, users, comments }

// const article = new schema.Entity('articles')

// const user = new schema.Entity('users')

// const comment = new schema.Entity('comments')


// 然后这里注意到

// articles: 整个原始数据里 author 字段包含了用户信息、 comments 字段包含了评论信息
// comments: 评论信息里 commenter 字段包含了用户信息
// users: author 字段没有包含其他信息
// 所以需要明确相互的关系，改成这样


// const article = new schema.Entity('articles', { author, comments })
// const user = new schema.Entity('users')
// const comment = new schema.Entity('comments', { commenter })

const user = new schema.Entity('users')
const comment = new schema.Entity('comments', { commenter: user })
const article = new schema.Entity('articles', { author: user, comments: [comment] })

const mySchema = [article]

export const normalizedData = normalize(myData.articles, mySchema)

console.log(normalizedData)




